<template>
	<view>

		<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
			:maxCount="maxCount"></u-upload>
	</view>
</template>

<script>
	import {
		http_url
	} from "@/static/js/http.js"
	// import {
	// 	imgDel
	// } from "@/api/yupload/index"
	import {get} from "@/static/js/uilt/index.js"
	import {imgUpload} from "@/static/js/api/hemo.js"
	export default {
		props: {
			maxCount: {
				type: Number,
				default: 1
			}
		},
		data() {
			return {
				fileList1: [],
			}
		},
		methods: {
			// 删除图片
			async deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				// let res = await imgDel(event.file.id)
				this.$emit("input", JSON.stringify(this.fileList1))
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					let result = await this.uploadFilePromise(lists[i].url)
					console.log(result,"////////////////");
					result = JSON.parse(result)
					// result = result.data
					console.log(result,"__________");
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, {
						...result,
						url: result.msg
					})
					fileListLen++
				}
				let arr = this.fileList1.map(r => {
					return {
						'url': r.url,
						'id': r.id
					}
				})
				this.$emit("input", JSON.stringify(arr))
			},
			async uploadFilePromise(url) {
				 let res = await imgUpload(url)
				 return res.data
			
			},
		}

	}
</script>